package com.yunsc.media.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunsc.common.exception.YunscException;
import com.yunsc.common.utils.PageResult;
import com.yunsc.media.dao.YunscUploadFileDao;
import com.yunsc.media.dto.FileDTO;
import com.yunsc.media.dto.QueryFileDTO;
import com.yunsc.media.entity.YunscUploadFileEntity;
import com.yunsc.media.service.YunscUploadFileService;
import com.yunsc.media.utils.FormatUploadFile;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

import static com.yunsc.common.utils.PageResult.formatPageResult;
import static com.yunsc.media.utils.FormatUploadFile.formatFile;

@Slf4j
@Service("yunscUploadFileService")
public class YunscUploadFileServiceImpl extends ServiceImpl<YunscUploadFileDao, YunscUploadFileEntity> implements YunscUploadFileService {


    /**
     * 根据文件id查询文件信息
     *
     * @param fileId 文件id
     * @return 文件信息
     */
    @Override
    public YunscUploadFileEntity queryFileById(Integer fileId) {
        YunscUploadFileEntity file = this.getById(fileId);
        if (file != null) {
            formatFile(file);
            return file;
        }
        return null;
    }

    /**
     * 根据文件id列表查询文件信息
     *
     * @param imageIds 文件id列表
     * @return 文件信息列表
     */
    @Override
    public List<YunscUploadFileEntity> queryFilesByIds(List<Integer> imageIds) {
        // LambdaQueryWrapper<YunscUploadFileEntity> lqw = new LambdaQueryWrapper<>();
        // lqw.in(YunscUploadFileEntity::getFileId, imageIds);
        // lqw.eq(YunscUploadFileEntity::getIsDelete, 0);
        // List<YunscUploadFileEntity> files = this.list(lqw);
        // if (CollectionUtils.isEmpty(files)) {
        //     return null;
        // }
        // files.forEach(FormatUploadFile::formatFile);
        List<YunscUploadFileEntity> files = new ArrayList<>();
        imageIds.forEach(id -> {
            YunscUploadFileEntity file = this.getById(id);
            if (file != null) {
                formatFile(file);
                files.add(file);
            }
        });
        return files;
    }

    @Override
    public PageResult<YunscUploadFileEntity> queryFileList(QueryFileDTO fileDTO, Integer storeId) {
        LambdaQueryWrapper<YunscUploadFileEntity> lqw = new LambdaQueryWrapper<>();
        // 根据分组id查询
        Integer groupId = fileDTO.getGroupId();
        if (groupId != null && groupId > 0) {
            lqw.eq(YunscUploadFileEntity::getGroupId, groupId);
        }
        // 根据文件类型查询
        if (fileDTO.getFileType() != null && fileDTO.getFileType() != -1) {
            lqw.eq(YunscUploadFileEntity::getFileType, fileDTO.getFileType());
        }
        // 根据上传来源
        if (fileDTO.getChannel() != null && fileDTO.getChannel() != -1) {
            lqw.eq(YunscUploadFileEntity::getChannel, fileDTO.getChannel());
        }
        // 根据存储类型查询
        lqw.eq(StringUtils.isNotEmpty(fileDTO.getStorage()), YunscUploadFileEntity::getStorage, fileDTO.getStorage());

        lqw.eq(YunscUploadFileEntity::getIsDelete, 0);
        // 根据文件名查询
        lqw.like(StringUtils.isNotEmpty(fileDTO.getFileName()), YunscUploadFileEntity::getFileName, fileDTO.getFileName());

        // 查询数据
        Page<YunscUploadFileEntity> pageResult = this.page(new Page<>(fileDTO.getPage(), fileDTO.getPageSize()), lqw);
        List<YunscUploadFileEntity> records = pageResult.getRecords();
        records.forEach(FormatUploadFile::formatFile);
        // 格式化数据
        return formatPageResult(records, fileDTO.getPage(), fileDTO.getPageSize(), pageResult.getTotal());

    }

    @Override
    @Transactional
    public void editFile(FileDTO fileDTO) {
        // 查询文件信息
        YunscUploadFileEntity file = this.getById(fileDTO.getFileId());
        if (file == null) {
            throw new YunscException("文件不存在");
        }
        // 更新文件信息
        BeanUtils.copyProperties(fileDTO.getForm(), file);
        file.setFileId(fileDTO.getFileId());
        this.updateById(file);
    }

    @Override
    @Transactional
    public void deleteFile(List<Integer> fileIds) {
        Integer integer = this.baseMapper.editIsDeleteById(fileIds);
        if (integer == null || integer == 0) {
            throw new YunscException("删除失败");
        }
    }

    @Override
    public String queryFileUrlById(Integer imageId) {
        if (imageId == null || imageId <= 0){
            return null;
        }
        YunscUploadFileEntity fileEntity = this.queryFileById(imageId);
        if (fileEntity == null){
            return null;
        }
        return fileEntity.getExternalUrl();
    }

}